/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_seek_callback_stub.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
SleSeekCallbackStub::SleSeekCallbackStub()
{
    memberFuncMap_[SleSeekCallbackInterfaceCode::SLE_CALLBACK_SEEK_STATE_CHANGE] = 
        &SleSeekCallbackStub::OnSeekStateChangeInner;
    memberFuncMap_[SleSeekCallbackInterfaceCode::SLE_CALLBACK_SEEK_RESULT] = 
        &SleSeekCallbackStub::OnSeekResultInner;
}

SleSeekCallbackStub::~SleSeekCallbackStub(){};

int SleSeekCallbackStub::OnRemoteRequest(
    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    if(code != SLE_CALLBACK_SEEK_RESULT) {  //不打印SLE_CALLBACK_SEEK_RESULT
        SLE_LOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
    }
    std::u16string descriptor = SleSeekCallbackStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    SLE_ASSERT_AND_RETURN_RET_LOG(descriptor == remoteDescriptor, ERR_INVALID_STATE, "local descriptor is not equal to remote error!");

    auto itFunc = memberFuncMap_.find((SleSeekCallbackInterfaceCode)code);
    if (itFunc != memberFuncMap_.end()) {
        auto memberFunc = itFunc->second;
        if (memberFunc != nullptr) {
            return (this->*memberFunc)(data, reply);
        }
    }
    SLE_LOGW("default case, need check.");
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}

int SleSeekCallbackStub::OnSeekStateChangeInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnSeekStateChangeInner In");
    int32_t state;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadInt32(state), TRANSACTION_ERR, "read sle state failed!");
    OnSeekStateChange(state);
    return NO_ERROR;
}

int SleSeekCallbackStub::OnSeekResultInner(MessageParcel &data, MessageParcel &reply)
{
#if 0
    SLE_LOGD("OnSeekResultInner In");
#endif
    std::shared_ptr<SeekResult> result(data.ReadParcelable<SeekResult>());
    SLE_ASSERT_AND_RETURN_RET_LOG(result, TRANSACTION_ERR, "read seek result failed!");
    OnSeekResult(*result);
    return NO_ERROR;
}
} // SleStandard
} // NearLink
} // OHOS